/*
 *			(c) by hamtitmapti@Smartxx.com
 *
 */

#include <sys/param.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/signal.h>
#include <time.h>

#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <dirent.h>

const char http_html_ReplyHeader[] = "HTTP/1.0 200 OK\r\nServer: X-Selector 1.0\r\n";


//Content-type: text/html\r\n\r\n";
/*
    open(HEADER, "> /tmp/header") || die $!;
    if($file =~ /404.html/) {
      print(HEADER "HTTP/1.0 404 File not found\r\n");
    } else {
      print(HEADER "HTTP/1.0 200 OK\r\n");
    }
    print(HEADER "Server: uIP/0.9 (http://dunkels.com/adam/uip/)\r\n");
    if($file =~ /\.html$/) {
	print(HEADER "Content-type: text/html\r\n");
    } elsif($file =~ /\.gif$/) {
	print(HEADER "Content-type: image/gif\r\n");
    } elsif($file =~ /\.png$/) {
	print(HEADER "Content-type: image/png\r\n");
    } elsif($file =~ /\.jpg$/) {
	print(HEADER "Content-type: image/jpeg\r\n");
    } else {
	print(HEADER "Content-type: text/plain\r\n");
    }
    print(HEADER "\r\n");
    close(HEADER);
  */  
    
int main() {
        DIR *dirp;
        struct dirent *dp;
        FILE *f;
        FILE *finp;
        int filesize;
        unsigned char *errorpage;
        int counter;
        int pagecounter;
        char szTemp[100];
        int i;
        unsigned char hash;
         
        dirp = opendir(".");
        while( (dp = readdir(dirp)) != NULL)
        {
                  //printf("%s\n", dp->d_name);
         
        }

    	f = fopen("http_pages.h", "wb");
    	if (f!=NULL) 
    	{    
		#if 0
		// Index Page
		finp = fopen("error.html", "rb");
    		if (finp!=NULL) 
    		{    
        		fseek(finp, 0, SEEK_END); 
        		filesize = ftell(finp); 
        		fseek(finp, 0, SEEK_SET);
        		errorpage = (unsigned char*) malloc(filesize);
		     	fread(errorpage, 1, filesize, finp);
 		        fclose(finp);
       		} else return 1;
        
        

		fprintf(f,"const char https_not_found_page[] = {\n");

		
		for (counter=0; counter<filesize ; counter++) {
      	        	fprintf(f,"0x%02x,",errorpage[counter]); 
      	        	if (counter%16==15) fprintf(f,"\n"); 
      	        	
      	       	}
       		fprintf(f,"0x00 \n};\n\n");
	        free(errorpage);
 	        
 	        // Index Page OPS
 	        #endif
 	    #if 0    
 		// Index Page OPS
		finp = fopen("error_OPS.html", "rb");
    		if (finp!=NULL) 
    		{    
        		fseek(finp, 0, SEEK_END); 
        		filesize = ftell(finp); 
        		fseek(finp, 0, SEEK_SET);
        		errorpage = (unsigned char*) malloc(filesize);
		     	fread(errorpage, 1, filesize, finp);
 		        fclose(finp);
       		} else return 1;

		fprintf(f,"const char https_not_found_page_OPS[] = {\n");
		
		for (counter=0; counter<filesize ; counter++) {
      	        	fprintf(f,"0x%02x,",errorpage[counter]); 
      	        	if (counter%16==15) fprintf(f,"\n"); 
      	        	
      	       	}
       		fprintf(f,"0x00 \n};\n\n");
	        free(errorpage);	        
 	    #endif    
 	        
 	        
 	        
 	        
 	        
		for (pagecounter=1;pagecounter<2;pagecounter++) {
			sprintf(szTemp,"p%d.html",pagecounter);
			
			finp = fopen(szTemp, "rb");
    			if (finp!=NULL) 
    			{    
        			fseek(finp, 0, SEEK_END); 
        			filesize = ftell(finp); 
        			fseek(finp, 0, SEEK_SET);
        			errorpage = (unsigned char*) malloc(filesize);
		     		fread(errorpage, 1, filesize, finp);
 		        	fclose(finp);
       			} else return 1;
       			
       			fprintf(f,"const char http_www_%d_page[] = {\n",pagecounter);

			fprintf(f,"\n");
		
			for (counter=0; counter<filesize ; counter++) {
      	        		fprintf(f,"0x%02x,",errorpage[counter]); 
      	        		if (counter%16==15) fprintf(f,"\n"); 
      	       		}
	       		fprintf(f,"0x00 \n};\n\n");
		        free(errorpage);
		}


		for (pagecounter=1;pagecounter<7;pagecounter++) {
			sprintf(szTemp,"img%d.gif",pagecounter);

			
			finp = fopen(szTemp, "rb");
    			if (finp!=NULL) 
    			{    
        			fseek(finp, 0, SEEK_END); 
        			filesize = ftell(finp); 
        			fseek(finp, 0, SEEK_SET);
        			errorpage = (unsigned char*) malloc(filesize);
		     		fread(errorpage, 1, filesize, finp);
 		        	fclose(finp);
       			} else return 1;
       			
       			fprintf(f,"const char http_img_%d_gif[] = {\n",pagecounter);

			fprintf(f,"\n");
		
			for (counter=0; counter<filesize ; counter++) {
      	        		fprintf(f,"0x%02x,",errorpage[counter]); 
      	        		if (counter%16==15) fprintf(f,"\n"); 
      	       		}
	       		fprintf(f,"0x00 \n};\n\n");
		        free(errorpage);
		}


		for (pagecounter=1;pagecounter<2;pagecounter++) {
			sprintf(szTemp,"img%d.png",pagecounter);

			
			finp = fopen(szTemp, "rb");
    			if (finp!=NULL) 
    			{    
        			fseek(finp, 0, SEEK_END); 
        			filesize = ftell(finp); 
        			fseek(finp, 0, SEEK_SET);
        			errorpage = (unsigned char*) malloc(filesize);
		     		fread(errorpage, 1, filesize, finp);
 		        	fclose(finp);
       			} else return 1;
       			
       			fprintf(f,"const char http_img_%d_png[] = {\n",pagecounter);

			fprintf(f,"\n");
		
			for (counter=0; counter<filesize ; counter++) {
      	        		fprintf(f,"0x%02x,",errorpage[counter]); 
      	        		if (counter%16==15) fprintf(f,"\n"); 
      	       		}
	       		fprintf(f,"0x00 \n};\n\n");
		        free(errorpage);
		}


    
 	        fclose(f);     
 	        
        }		


        (void) closedir(dirp);  
        
	printf("Ready\n");
       
       
       	
	
}
